/**
 * 通用可配置的列模型
 */
$import("config");

config.ColumnModel = function(cfg) {
	
	// 获取表格配置
	var table = cfg.tableConfig;
	var tableColumns = [];
	var ef = Ext.form;
	for (var key in table.fields) {
		if (table.fields[key].viewStrategy == 'NONE')
			continue;
		var field = table.fields[key];
		var FieldEditor;
        var defaultCfg = {
        	allowBlank: false
        };
        
        // 编辑字段配置
        var editorCfg = {}, columnCfg = {};
        // 配置不同类型的编辑器
        if (field.dataType == 'INTEGER' || field.dataType == 'LONG') {
        	FieldEditor = ef.NumberField;
        	editorCfg = Ext.apply(editorCfg, {
        		allowDecimals: false
        	}); 
        	columnCfg = Ext.apply(editorCfg, {
        		xtype : 'numbercolumn'
        	});
        } else if (field.dataType == 'FLOAT' || field.dataType == 'DOUBLE') {
        	FieldEditor = ef.NumberField;
        } else if (field.dataType == 'DATE') {
        	FieldEditor = ef.DateField;
        	editorCfg = Ext.apply(editorCfg, {
        		format: 'Y-m-d'
        	}); 
        	columnCfg = Ext.apply(editorCfg, {
        		xtype : 'datecolumn',
        		format: 'Y-m-d'
        	});
        } else if (field.dataType == 'DATETIME') {
        	FieldEditor = ef.DateField;
        	editorCfg = Ext.apply(editorCfg, {
        		format: 'Y-m-d H:m:s'
        	});
        	columnCfg = Ext.apply(editorCfg, {
        		xtype : 'datecolumn',
        		format: 'Y-m-d H:m:s'
        	});
        } else if (field.dataType == 'BOOLEAN') {
        	FieldEditor = ef.RadioGroup;
        	editorCfg = Ext.apply(editorCfg, {
        		items : [{
        			name: 'sex',
        			boxLabel: '男',
        			inputValue: true
        		}, {
        			name: 'sex',
        			boxLabel: '女',
        			inputValue: false
        		}]
        	});
        } else {
        	FieldEditor = ef.TextField;
        }
        
        editorCfg = Ext.apply(defaultCfg, editorCfg);
        var fieldEditor = new FieldEditor(editorCfg);
        fieldEditor.fieldInfo = field;
        
        columnCfg = Ext.apply({
	        header: field.alias,
	        dataIndex: field.name,
	        editor: fieldEditor
	    }, columnCfg);
        
		tableColumns[tableColumns.length] = columnCfg;
	}
	
	// 创建表格列模型
	var cm = new Ext.grid.ColumnModel({
		defaults: {
		    sortable: true,
		    width: 70
		},
		columns: tableColumns
	});
	
	return cm;
};


